import 'package:flutter/material.dart';
import 'package:local_coffee/components/hex.dart';
import 'package:local_coffee/components/icon.dart';
import 'package:local_coffee/components/rgba.dart';
import 'package:local_coffee/pages/toolbar/home/index.dart';
import 'package:local_coffee/pages/toolbar/menu/index.dart';
import 'package:local_coffee/pages/toolbar/mine/index.dart';
import 'package:local_coffee/pages/toolbar/order/index.dart';
import 'package:local_coffee/pages/toolbar/shopping_cart/index.dart';
import 'package:local_coffee/routes/index.dart';

class ToolBar extends StatefulWidget {
  final String routeName;
  final Object arguments;

  // 初始化所有的 toolBar 页面
  static Home _home = Home();
  static Menu _menu = Menu();
  static Order _order = Order();
  static ShoppingCart _shoppingCart = ShoppingCart();
  static Mine _mine = Mine();

  /// 所有 toolbar 页面
  final Map<int, Map> pages = {
    0: _createPage(_home, appBar: _home.getAppBar(), routeName: N.root),
    1: _createPage(_menu, appBar: _menu.getAppBar(), routeName: N.menu),
    2: _createPage(_order, appBar: _order.getAppBar(), routeName: N.order),
    3: _createPage(_shoppingCart, appBar: _shoppingCart.getAppBar(), routeName: N.shoppingCart),
    4: _createPage(_mine, appBar: _mine.getAppBar(), routeName: N.mine),
  };

  /// 创建页面 Map
  /// ```
  /// @param {Widget} page - 页面
  /// @param {Appbar} appbar - 当前页面是否显示appbar 默认为true
  /// ```
  static Map _createPage(
    Widget page,
    {
      AppBar appBar,
      String routeName
    }
  ) {
    return {
      'widget': page,
      'appBar': appBar,
      'routeName': routeName
    };
  }

  static ToolBar _singleton;

  ToolBar.singleton({
    this.routeName,
    this.arguments
  });

  factory ToolBar({
    String routeName,
    Object arguments
  }) {
    if (_singleton == null) {
      _singleton = ToolBar.singleton(
        routeName: routeName,
        arguments: arguments,
      );
    }

    return _singleton;
  }

  /// 获取 routeName 获取对应页面的索引
  getPageIndex(String name) {
    switch (name) {
      case N.menu:
        return 1;
      case N.order:
        return 2;
      case N.shoppingCart:
        return 3;
      case N.mine:
        return 4;
      default:
        return 0;
    }
  }

  _NavigationState createState() => _NavigationState();
}

class _NavigationState extends State<ToolBar> {
  
  static int currentIndex = 0;

  @override
  void initState() {
    super.initState();

    Future.delayed(Duration.zero, () {
      String routeName = ModalRoute.of(context).settings.name;

      setState(() {
        currentIndex = widget.getPageIndex(routeName);
      });
    });
  }

  @override
  Widget build(BuildContext context) {

    Map page = widget.pages[currentIndex];

    return Scaffold(
      appBar: page['appBar'],
      body: Container(
        color: HEX.hexfff,
        child: page['widget'],
      ),
      bottomNavigationBar: Theme(
        data: ThemeData(
          highlightColor: Colors.transparent,
          splashColor: Colors.transparent
        ),
        child: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
              icon: icon0xe605(),
              title: Text('首页')
            ),
            BottomNavigationBarItem(
              icon: icon0xe602(),
              title: Text('菜单')
            ),
            BottomNavigationBarItem(
              icon: icon0xe600(),
              title: Text('订单')
            ),
            BottomNavigationBarItem(
              icon: icon0xe606(),
              title: Text('购物车')
            ),
            BottomNavigationBarItem(
              icon: icon0xe603(),
              title: Text('私')
            )
          ],
          unselectedFontSize: 10, // 未选中字体大小
          selectedFontSize: 10, // 选中字体大小
          selectedItemColor: RGBA.rgba2b4c73,
          currentIndex: currentIndex,
          onTap: (index) {
            setState(() {
              currentIndex = index;
            });
          },
        ),
      ),
    );
  }
}
